/**
 * Created by no1 on 2015/12/21.
 */
'use strict';
let DD = require('./daoLoader');

let F = require('../function');
let Base = require('./Base');
let util = require("util");
let ReceiveLogMongoDao = new DD.mongoDB.ReceiveLogDao();
let userDao = new DD.mysqlDB.UserDao();

function ReceiveLogDispatcher(data) {
  data = data || {};
  data.dao = data.dao || ReceiveLogMongoDao;
  data.esDao = data.esDao || {};
  data.redisDao = data.redisDao || {};
  data.modelName = data.modelName || 'coupon';
  data.modelType = data.modelType || 'mongo';
  this.data = data;
  Base.call(this, this.data);
}
util.inherits(ReceiveLogDispatcher, Base);
let DP = ReceiveLogDispatcher.prototype;

/**
 * @todo 添加
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.add = function*(fields) {
  if (!fields.uid) return F.returnData(1, '必须指定用户id');
  if (!fields.couponid) return F.returnData(1, '必须指定优惠券id');
  let parameters = {}
    , arr = ['uid', 'nickname', 'mobile', 'city', 'couponid', 'couponCode', 'couponName', 'money']
    , ret, retCoupon;
  //默认添加
  if (fields.act == 1) {
    for (let one of arr) if (fields[one] != null) parameters[one] = fields[one];
    parameters.status = 1;

    //查询优惠券
    retCoupon = yield F.getDataFromDispatcher('couponDispatcher', 'findByDis', {id: fields.couponid});
    if (retCoupon.error == 1) return F.returnData(1, '优惠券查询有误');
    let coupon = retCoupon.data[0];
    parameters.couponCode = coupon.couponCode;
    parameters.couponName = coupon.name;
    parameters.money = coupon.money;

    //查询用户
    let retUser = yield userDao.getUserInfo(fields.uid);
    if (retUser.error == 1) return F.returnData(1, '查询用户信息有误');
    let user = retUser.data[0];
    parameters.nickname = user.nickname;
    parameters.mobile = user.mobile;
  }
  ret = yield this.dao.findAndSave({id: 0}, parameters);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  return F.returnData(0, 'ok', ret.data);
};

/**
 * todo 更新优惠券领取日志
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.update = function*(fields) {
  if (!fields.id) return F.returnData(1, '必须制定日志的id');
  let ret, retData, parameters = {};
  if (fields.act = 1) {
    if (fields.status == null) return F.returnData(1, '必须指定状态');
    parameters.status = fields.status;
  }
  //更新数据
  ret = yield this.dao.findAndUpdate({id: fields.id}, parameters, true);
  if (ret.error == 1) return F.returnData(1, ret.msg);

  let json = JSON.parse(JSON.stringify({data: ret.data}));
  retData = F.dataFilter(json, ['_id', 'createdAt', 'updateAt', '__v', '__t']);
  return F.returnData(0, ret.msg, retData.data);
};

/**
 * todo 获取优惠券日志的列表
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.list = function*(fields) {
  let ret, retData = {}, opt = {}, retCount, retDataList = [], parameters = {}
    , page = fields.page || 1
    , pageSize = fields.pageSize
    , isSort = {id: -1}
    , arrDel = ['_id', '__v'];
  for (let one of arrDel) opt[one] = 0;
  if(fields.act==1){
    if (!!fields.vendorid) {
      let retCoupons = yield F.getDataFromDispatcher('couponDispatcher', 'findByDis', {vendorid: fields.vendorid});
      if (retCoupons.error == 1) return F.returnData(1, retCoupons.msg);
      let couponids = [];
      for (let one of retCoupons.data) couponids.push(one.id);
      parameters.couponid = {$in: couponids};
    }
    ret = yield this.dao.getByQueryPor(parameters, opt, {page: page, pageSize: pageSize}, isSort);
    if (ret.error == 1) return F.returnData(1, ret.msg);
    retDataList = ret.data;
    if (fields.isPaging == 1) retCount = yield this.dao.getCountByQuery(parameters);
    if (fields.isPaging == 1) {
      retData.list = retDataList;
      retData.count = retCount.data;
    } else retData = retDataList;
    return F.returnData(0, ret.msg, retData);
  }
  else if(fields.act ==2){
    if(!fields.couponid) return F.returnData(1,'必须有couponid');
    parameters.couponid = fields.couponid;
    ret = yield this.dao.getByQueryPor(parameters, opt, {page: page, pageSize: pageSize}, isSort);
    if (ret.error == 1) return F.returnData(1, ret.msg);
    retDataList = ret.data;
    if (fields.isPaging == 1) retCount = yield this.dao.getCountByQuery(parameters);
    if (fields.isPaging == 1) {
      retData.list = retDataList;
      retData.count = retCount.data;
    } else retData = retDataList;
    return F.returnData(0, ret.msg, retData);
  }

};

/**
 * todo 获取优惠券日志的详情
 * @param fields
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.detail = function*(fields) {
  if (!fields.id) return F.returnData(1, '必须指定id');
  let ret, opt = {}, arrDel = ['_id', '__v'];
  for (let one of arrDel) opt[one] = 0;
  ret = yield this.dao.getByQuery({id: fields.id}, opt);
  if (ret.error == 1 || ret.data.length != 1) return F.returnData(1, '查询有误');
  return F.returnData(0, ret.msg, ret.data[0]);
};


/**
 * 内部调用
 * @param Qut
 * @returns {{error, msg, data}|{error, msg}}
 */
DP.findByDis = function*(Que) {
  let arr = ['id', 'name', 'status']
    , opt = {};
  for (let one of arr) opt[one] = 1;
  let ret = yield  this.dao.getByQuery(Que, opt);
  let json = JSON.parse(JSON.stringify({data: ret.data}));
  let retData = F.dataFilter(json, ['_id']);
  if (ret.error == 1) return F.returnData(1, ret.msg, retData.data);
  return F.returnData(0, ret.msg, retData.data);
};

module.exports = ReceiveLogDispatcher;